package com.jxwifi.cloud.quickclean.quicktreasure;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.TabPageAdapter;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.PageVoBean;
import com.jxwifi.cloud.quickclean.bean.QuickTreBean;
import com.jxwifi.cloud.quickclean.fragment.QuickListFragment;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.popwindow.PopPackages;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.popwindow.PopWindowUtil;
import com.popwindow.Popwindows;

import java.util.ArrayList;
import java.util.List;

public class QuickPresenter {

    private Context mContext;
    private QuickTreasureView mView;
    private AuzImpl mAuz;

    private String[] mTabNames = {StateUtils.QUICK_RECHARGE, StateUtils.QUICK_CONSUMPTION};

    private int indexPage = 1;
    private int mLimit = 10;

    private PopPackages mModule;

    public QuickPresenter(Context context, QuickTreasureView view) {
        this.mContext = context;
        this.mView = view;
        mAuz = new AuzImpl();
    }

    public void fgView(QuickTreBean mBean) {

        if (StateUtils.NORMAL.equals(mBean.getUcCustomerBank().getStatus())) {
            mView.getBalance().setText("可以使用套餐余额");
        } else {
            mView.getBalance().setText("可以使用套餐余额(账户被冻结或异常，请联系我们)");
        }

        List<Fragment> fragment = new ArrayList<Fragment>();
        for (int i = 0; i < 2; i++) {
            QuickListFragment mOrderFragment = new QuickListFragment();
            Bundle bundle = new Bundle();
            bundle.putString("product_price_code".toLowerCase(), mTabNames[i]);
            if (i == 0) {
                bundle.putSerializable("inrecordbean".toLowerCase(), mBean.getInRecord());
            } else {
                bundle.putSerializable("inrecordbean".toLowerCase(), mBean.getOutRecord());
            }
            mOrderFragment.setArguments(bundle);
            Log.w("oushuhua", "getProductPriceCode ");
            fragment.add(mOrderFragment);
        }
        TabPageAdapter pageAdapter = new TabPageAdapter(mView.getFrgaments(), fragment, mTabNames);
        pageAdapter.notifyDataSetChanged();
        mView.getPage().setAdapter(pageAdapter);
        mView.getTabLayouts().setupWithViewPager(mView.getPage());
        mView.getTabLayouts().setSelectedTabIndicatorHeight(0);

        //字体颜色的设置第一种写法无效，需要用第二种才能生效
        //mView.getTabLayouts().setTabTextColors(R.color.color_white,R.color.color_text5);
        mView.getTabLayouts().setTabTextColors(Color.parseColor("#A7A7A7"), Color.parseColor("#3DB64D"));
        //设置监听
        mView.getTabLayouts().addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //tab项选中状态时执行

                mView.getPage().setCurrentItem(tab.getPosition());
                View view = tab.getCustomView();
                if (null != view && view instanceof TextView) {
                    ((TextView) view).setTextColor(ContextCompat.getColor(mContext, R.color.color_text5));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //tab项取消选中状态时执行
                // Toast.makeText(mContext, "第"+tab.getPosition()+"tab选项的onTabUnselected", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //tab项选中状态再次点击时执行
                //Toast.makeText(mContext, "第"+tab.getPosition()+"tab选项的onTabReselected", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void initData() {
        PageVoBean mBean = new PageVoBean();
        mBean.setPage(indexPage);
        mBean.setLimit(mLimit);
        Params params = new Params();
        params.add("pageVo", mBean);
        params.add("type", "NONE");
        mAuz.auzpost(UrlPool.POST_BANK_LOOKUP, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    QuickTreBean mBean = JSON.parseObject(data, QuickTreBean.class);
                    mView.getQuickPrice().setText("\u00A5" + mBean.getUcCustomerBank().getSurplusAmount());
                    fgView(mBean);
                } else {
                    mView.oninitData();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    public void RedemptionPackage() {
        mModule = new PopPackages(mContext, new PopPackages.PopPackagesListener() {
            @Override
            public void PopAgree(String RedemptionCode) {
                Redemption(RedemptionCode);
            }
        });
        Popwindows window = PopWindowUtil.getPopupWindow(mModule.getView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                .LayoutParams.MATCH_PARENT);
        mModule.setPopwindow(window);
        window.show(mView.getQuickPrice(), Popwindows.Location.TOP);
    }

    public void Redemption(String RedemptionCode) {
        Params params = new Params();
        params.add("rechargePwd", RedemptionCode);
        mAuz.auzpost(UrlPool.POST_ECCHANGE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {

                Toast toast = Toast.makeText(mContext, "兑换成功", Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
                initData();
                if (mModule != null)
                    mModule.mPopwindows.dismiss();
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

}
